﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.Models.View
{
    /// <summary>
    /// 交易员年累计
    /// </summary>
    public class OperatorYearSum
    {
        public int Id { get; set; }

        /// <summary>
        /// 用户编号
        /// </summary>
        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        /// <summary>
        /// 交易年
        /// </summary>
        public int Year { get; set; }
        /// <summary>
        /// 交易月
        /// </summary>
        public int Month { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }

        /// <summary>
        /// 资金利用率
        /// </summary>
        public double Utilization { get; set; }
        /// <summary>
        /// 胜率
        /// </summary>
        public double WinRate { get; set; }
        /// <summary>
        /// 涨幅
        /// </summary>
        public double RaiseRate { get; set; }

        /// <summary>
        /// 每笔盈利
        /// </summary>
        public double AvgBillBenefit { get; set; }

        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }

        /// <summary>
        /// 风险次数
        /// </summary>
        public int RiskCount { get; set; }

        /// <summary>
        /// 收益率
        /// </summary>
        public double Yield { get; set; }
        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }
        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }
        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }

        //public void InitOperatorYearSum(int tradeHistoryId, DB.DBManager db)
        //{
        //    List<BenefitHelper.Sys.Operator> operators = db.Operators.ToList();
        //    foreach (BenefitHelper.Sys.Operator op in operators)
        //    {
        //        SetOperatorYearSum(op.Id, tradeHistoryId, db);
        //    }
        //    db.SaveChanges();
        //}

        //public void Delete(int year, DB.DBManager db)
        //{
        //    var query = db.OperatorYearSum.Where(a => a.Year == year).ToList();
        //    foreach (OperatorYearSum s in query)
        //    {
        //        db.OperatorYearSum.Remove(s);
        //    }
        //}

        //public OperatorYearSum GetOperatorYearSum(int operatorid, int year, DB.DBManager db)
        //{
        //    var query = db.OperatorYearSum.Where(a => a.OperatorId == operatorid).Where(a => a.Year == year).ToList();
        //    if (query.Count() > 0)
        //    {
        //        return query.First();
        //    }
        //    else
        //    {
        //        return null;
        //    }

        //}

        ///// <summary>
        ///// 获取操作员年数据
        ///// </summary>
        ///// <param name="operatorid"></param>
        ///// <param name="week"></param>
        ///// <param name="month"></param>
        ///// <param name="year"></param>
        ///// <returns></returns>
        //public void SetOperatorYearSum(int operatorid, int tradeHistoryId, DB.DBManager db)
        //{
        //    OperatorYearSum change = new OperatorYearSum();
        //    BenefitHelper.Sys.TradeHistory th = db.TradeHistorys.Find(tradeHistoryId);
        //    change.Year = th.YPdate;
        //    change.Month = th.MPdate;
        //    change.OperatorId = operatorid;
        //    var queryExist = GetOperatorYearSum(operatorid, th.YPdate, db);
        //    List<BenefitHelper.Sys.TradeHistory> historys = new BenefitHelper.Sys.TradeHistory().GetMonthTradeingDay(th.MPdate, th.YPdate, db);
        //    //List<DateTime> tradingdays = new List<DateTime>();
        //    //foreach (BenefitHelper.Sys.TradeHistory h in historys)
        //    //{
        //    //    tradingdays.Add(Convert.ToDateTime(h.YPdate + "-" + h.MPdate + "-" + h.DPdate));
        //    //}
        //    int allcount = 0;
        //    foreach (BenefitHelper.Sys.TradeHistory date in historys)
        //    {
        //        List<Sys.Account> accounts = new Sys.DayOperatorAccount().GetDayOperatorAccounts(th.GetDateTimeFromTradeHistoryId(tradeHistoryId, db), operatorid, db);
        //        if (accounts.Count > 0)
        //        {
        //            foreach (Sys.Account account in accounts)
        //            {
        //                var query = from t in db.AccountMonthChange where t.AccountId == account.Id && t.Month == date.MPdate && t.Year == date.YPdate select t;
        //                if (query.Count() > 0)
        //                {
        //                    allcount++;
        //                    change.BillCount += query.First().BillCount;
        //                    change.DayCount += query.First().DayCount;
        //                    change.Free += query.First().Free;
        //                    change.LoseCount += query.First().LoseCount;
        //                    change.OperatorId = operatorid;
        //                    change.Profit += query.First().Profit;
        //                    change.SumLost += query.First().SumLost;
        //                    change.SumWin += query.First().SumWin;
        //                    change.UsedMargin += query.First().UsedMargin;
        //                    change.WinCount += query.First().WinCount;
        //                }


        //                var _query = from t in db.AccountMonthAnalysis where t.AccountId == account.Id && t.Month == date.MPdate && t.Year == date.YPdate select t;
        //                if (query.Count() > 0)
        //                {
        //                    change.AvgBillBenefit += _query.First().AvgBillBenefit;
        //                    change.AvgPostionTime += _query.First().AvgPostionTime;
        //                    change.MaxReturn += _query.First().MaxReturn;
        //                    change.OverStopLoss += _query.First().OverStopLoss;
        //                    change.PingJinPing += _query.First().PingJinPing;
        //                    change.RaiseRate += _query.First().RaiseRate;
        //                    change.RiskCount += _query.First().RiskCount;
        //                    change.Utilization += _query.First().Utilization;
        //                    change.WinRate += _query.First().WinRate;
        //                    change.Yield += _query.First().Yield;
        //                }
        //            }
        //        }
        //    }
        //    if (queryExist != null)
        //    {
        //        queryExist.AvgBillBenefit = change.AvgBillBenefit;
        //        queryExist.AvgPostionTime = change.AvgPostionTime;
        //        queryExist.BillCount = change.BillCount;
        //        queryExist.DayCount = change.DayCount;
        //        queryExist.Free = change.Free;
        //        queryExist.LoseCount = change.LoseCount;
        //        queryExist.MaxReturn = change.MaxReturn;
        //        queryExist.OverStopLoss = change.OverStopLoss;
        //        queryExist.PingJinPing = change.PingJinPing;
        //        queryExist.Profit = change.Profit;
        //        queryExist.RaiseRate = change.RaiseRate;
        //        queryExist.RiskCount = change.RiskCount;
        //        queryExist.SumLost = change.SumLost;
        //        queryExist.SumWin = change.SumWin;
        //        queryExist.UsedMargin = change.UsedMargin;
        //        queryExist.Utilization = change.Utilization;
        //        queryExist.WinCount = change.WinCount;
        //        queryExist.WinRate = change.WinRate;
        //        queryExist.Yield = change.Yield;
        //    }
        //    else
        //    {
        //        db.OperatorYearSum.Add(change);
        //    }
        //}



        //public List<View.OperatorYearSum> GetGroupOperatorYearChange(int groupId, int year, DB.DBManager db)
        //{
        //    Sys.TradeHistory th = new Sys.TradeHistory();
        //    th = th.GetYearLastTradingDay(year, db);
        //    List<Sys.Operator> operators = new Sys.DayGroupOperators().GetGroupOperators(groupId, Convert.ToDateTime(th.YPdate + "-" + th.MPdate + "-" + th.DPdate), db);
        //    List<View.OperatorYearSum> changes = new List<View.OperatorYearSum>();
        //    foreach (Sys.Operator op in operators)
        //    {
        //        View.OperatorYearSum change = new View.OperatorYearSum().GetOperatorYearSum(op.Id, th.YPdate, db);
        //        changes.Add(change);
        //    }
        //    return changes;
        //}
    }
}
